/**
 * 
 */
package com.cgbsoft.financial;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.cgbsoft.financial.common.ConfigurationManager;

/**
 * @author CX
 *
 */
public class PasswordSetting extends Activity implements OnClickListener {
	
	public final static String TAG = "PasswordSetting";
	
	private EditText oldPassword, password, passwordReinput;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.password_setting);
		
		oldPassword = (EditText) findViewById(R.id.old_password);
		password = (EditText) findViewById(R.id.password);
		passwordReinput = (EditText) findViewById(R.id.password_reinput);
		
		findViewById(R.id.submit_button).setOnClickListener(this);
		findViewById(R.id.cancel_button).setOnClickListener(this);
		findViewById(R.id.active_password).setOnClickListener(this);
		
		String hasPassword = ConfigurationManager.getInstance(this).getStringConfig(ConfigurationManager.NEED_PASSWORD, "");
		
		if ("true".equals(hasPassword)) {
			findViewById(R.id.old_password_layout).setVisibility(View.VISIBLE);
			((CheckBox)findViewById(R.id.active_password)).setChecked(true);
		} else {
			password.setEnabled(false);
			passwordReinput.setEnabled(false);
		}
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()) {
		case R.id.submit_button: 
			setPassword();
			break;
		case R.id.cancel_button: 
			this.finish();
			break;
		case R.id.active_password: 
			onActivePasswordClick();
			break;
		default:
			break;
		}
		
	}
	
	/**
	 * 检查密码，如果成功，跳转到主界面，如果不成功，提示重新输入
	 */
	private void setPassword() {
		String inputPassword = password.getText().toString();
		String inputPasswordReset = passwordReinput.getText().toString();
		checkPassword();
		
		if (TextUtils.isEmpty(inputPasswordReset)) {
			Toast.makeText(PasswordSetting.this, "密码不能为空喔~再试一次？", Toast.LENGTH_SHORT).show();
			return;
		}
		
		if (inputPassword.equals(inputPasswordReset) 
				&& !TextUtils.isEmpty(inputPasswordReset)) {
			//设置密码
			ConfigurationManager.getInstance(this).setStringConfig(ConfigurationManager.PASS_WORD, inputPassword);
			ConfigurationManager.getInstance(this).setStringConfig(ConfigurationManager.NEED_PASSWORD, "true");
			Toast.makeText(PasswordSetting.this, "密码设置成功了，下次要用密码登录喔~", Toast.LENGTH_SHORT).show();
			//返回主界面
			this.finish();
		} else {
			Toast.makeText(PasswordSetting.this, "两次密码要相同喔~再试一次？", Toast.LENGTH_SHORT).show();
		}
	}
	
	/**
	 * 启用密码
	 */
	private void onActivePasswordClick() {
		CheckBox activePassword = (CheckBox)findViewById(R.id.active_password);
		if (activePassword.isChecked()) {
			activePassword.setText("取消密码");
			password.setEnabled(true);
			passwordReinput.setEnabled(true);
		} else {
			checkPassword();
			activePassword.setText("启用密码");
			ConfigurationManager.getInstance(this).setStringConfig(ConfigurationManager.PASS_WORD, "");
			ConfigurationManager.getInstance(this).setStringConfig(ConfigurationManager.NEED_PASSWORD, "");
			password.setEnabled(false);
			passwordReinput.setEnabled(false);
		}
	}
	
	/**
	 * 检查密码
	 */
	private void checkPassword() {
		String old_password = oldPassword.getText().toString();
		String sysPassword = ConfigurationManager.getInstance(this).getStringConfig(ConfigurationManager.PASS_WORD, "");
		String hasPassword = ConfigurationManager.getInstance(this).getStringConfig(ConfigurationManager.NEED_PASSWORD, "");
		if ("true".equals(hasPassword)) {
			if (!old_password.equals(sysPassword)) {
				Toast.makeText(PasswordSetting.this, "旧密码不正确，要输正确才能设置新密码喔~", Toast.LENGTH_SHORT).show();
				return;
			}
		}
	}
}
